<template>
  <div class="post-comment">
    <van-field
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      :placeholder="`${comment.com_id? '回复 ' + comment.aut_name:'请输入留言'}`"
      show-word-limit
    />
    <van-button
      class="post-btn"
      type="default"
      size='small'
      @click="handlePost"
      :disabled='!message'
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment'
export default {
  name: 'PostComment',
  components: {},
  props: {
    target: {
      type: [Number, String, Number],
      required: true
    },
    articleId: {
      type: [Number, String, Number],
      default: null
    },
    comment: { // 只为回复三级 表单名字
      type: Object,
      default: () => { return {} }
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    async handlePost () {
      this.$toast.loading('发布中...')
      // 找到数据接口
      const { data } = await addComment({
        target: this.target.toString(), // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
        content: this.comment.com_id ? `${this.message}//@${this.comment.aut_name}:${this.comment.content}` : this.message, // 评论内容
        art_id: this.articleId === null ? null : this.articleId.toString() // 对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数
      })

      // this.$toast.fail('发布失败')
      this.$toast.success('发布成功..')
      this.message = ''
      this.$emit('post-success', data.data.new_obj)
      // 封装请求方法
      // 请求提交数据
      // 处理响应结果
    }
  }
}

</script>
<style lang='less' scoped>
.post-comment {
  padding: 14px;
  display: flex;
  align-items: center;
  .post-btn {
    flex-shrink: 0;
    margin-left: 14px;
  }
}
</style>
